var express = require('express');
var router = express.Router();
const { cuisineModel, orderModel, userModel, categoryModel,MenuModel,othershopModel,cuisineshellModel,UserOrderModel,AdminModel } = require('../model/model')

/* GET home page. */
router.get('/', function (req, res, next) {
  res.render('index', { title: 'Express' });
});
// 获取菜品分类
router.get('/getcate', async (req, res) => {
  let data = await categoryModel.find()
  res.send({
    code: 200,
    msg: '获取成功',
    data: data
  })
})
// 获取订单信息
router.get('/getordersy',async (req,res)=>{
  let data=await UserOrderModel.find()
// console.log(data);
  res.send({
    code:200,
    msg:'获取成功',
    data:data
  })
})
// 添加菜肴
router.post('/addcuisine', (req, res) => {
  cuisineModel.create(req.body)
  // .then((val)=>{
  //   console.log(val);
  // })
  res.send('添加成功')
})
// 获取菜品
router.get('/showcuisine', async (req, res) => {
  let data = await cuisineModel.find()
  res.send({
    code: 200,
    msg: '获取成功',
    data: data
  })

})
// 修改菜品
router.put('/updatecuisine', async (req, res) => {
  console.log(req.body)
  await cuisineModel.updateOne({ _id: req.body._id }, req.body)

  res.send({
    code: 200,
    msg: '修改成功'
  })
})
// 添加客户
router.post('/adduser', (req, res) => {
  userModel.create(req.body)
  res.send({
    code: 200,
    msg: "添加成功"
  })

})
// 获取客户
router.get('/showuser', async (req, res) => {
  let { page, pagesize } = req.query

  let data = await userModel.find().limit(pagesize).skip(page * pagesize - pagesize)
  let total = await userModel.find().count()
  res.send({
    code: 200,
    msg: '获取成功',
    data: data,
    total: total
  })

})

// 添加单独菜品表
router.post('/addmenu', (req, res) => {
  MenuModel.create(req.body)
  res.send({
    code: 200,
    msg: "添加成功"
  })
  
})


// 用户和点菜信息渲染
router.get('/list', async (req, res) => {
  let list = await userModel.aggregate([ //用户表 (用户表+点餐表)
    {
      $lookup: {
        from: 'menu', //需要关联的表
        localField: '_id', //当前user表中被关联的字段
        foreignField: 'pid',//外部表中被关联的字段
        as: 'user_list'//自定义的查询结果
      }
    }
  ])
  res.send({
    code: 200,
    list
  })
})

// 添加用户order表
router.post('/addorder', (req, res) => {
  UserOrderModel.create(req.body)
  res.send({
    code: 200,
    msg: "添加成功"
  })
})
// 获取userorder的分类信息
router.get('/categetuserorder',async (req,res)=>{
  let data=await UserOrderModel.aggregate([
    {
      $group:{
        _id:'$state',
        count:{$sum:1}
      }
    }
  ])
  console.log(data);
  res.send({
    code:200,
    data:data
  })
})


// 马欲蛟接口
// 获取用户接口
router.get('/myj/getUsers',async(req,res)=>{
  let data = await userModel.find()
  res.send({
    code:200,
    data
  })
})
// 获取其他店铺信息
router.get('/myj/getOtherShop',async(req,res)=>{
  let data = await othershopModel.find()
  res.send({
    code:200,
    data
  })
})
// 获取菜品接口
router.get('/myj/getCuisine',async(req,res)=>{
  let data = await cuisineModel.find()
  res.send({
    code:200,
    data
  })
})
// 添加VIP
router.post('/myj/addUser',async(req,res)=>{
  await userModel.create(req.body)
  res.send({
    code:200
  })
})

// 赵宁
// 获取所有员工信息
router.get('/getstaff', async (req, res) => {
  let data = await AdminModel.find()
  console.log(data);
  res.send({
    code: 200,
    msg: '获取成功',
    data: data
  })
})
// 登录接口
router.get('/login', async (req, res) => {
  console.log(req.query);
  await AdminModel.find(req.query).then(data => {
    if (data.length > 0) {
      res.send({
        code: 200,
        msg: '登录成功',
        data: data[0]
      })
    } else {
      res.send({
        code: 201,
      })
    }
  })
})
// 添加员工
router.post('/addstaff', async(req, res) => {
  let data=await AdminModel.find({userphone:req.body.userphone})
  if(data.length>0){
    res.send({
      code: 201,
      msg: '该手机号已注册'
    })
  }else{
    AdminModel.create(req.body)
    res.send({
      code: 200,
      msg: '添加成功'
    })
  }
})
// 获取订单表
router.get('/getuserOrder', async (req, res) => {
  let data = await UserOrderModel.find().populate('menuid')
  res.send({
    code: 200,
    msg: '获取成功',
    data: data
  })
})
// 获取菜品详情
router.get('/getmenus', async (req, res) => {
  let {id}=req.query
  let data = await UserOrderModel.findOne({userid:id}).populate('userid')
  let _id=data.userid._id
  console.log(_id);
  // console.log(data);

  let data2=await MenuModel.find({pid:_id})
  res.send({
    code: 200,
    msg: '获取成功',
    data: data2
  })
})


//修改user中的桌次
router.put('/update_user', async (req, res) => {
  let {_id,Table_times}=req.body
  console.log(_id,Table_times);
  await userModel.updateOne({_id},{Table_times})
  res.send({
    code: 200,
    msg: '修改成功'
  })
  
  
})

module.exports = router;
